<template>
  <div class="components-container">
    <!-- 查询条件 -->
    <div class="header-form">
      <div class="layui-inline" v-if="!showsecond">
        <label class="layui-form-label">名称</label>
        <div class="layui-input-inline">
          <el-select v-model="selectData.memNameLike" placeholder="请选择悼念厅" filterable @change="btnSearch">
            <el-option label="不选择" value=""></el-option>
            <el-option v-for="item in memNames" :key="item.value" :label="item.label" :value="item.label">
            </el-option>
          </el-select>
        </div>
      </div>
      <div class="layui-inline" v-if="showsecond">
        <label class="layui-form-label">名称</label>
        <div class="layui-input-inline">
          <el-cascader :options="memNames" filterable :show-all-levels="false" change-on-select v-model="MehselectArea" filterable></el-cascader>
        </div>
      </div>
      <div class="layui-inline">
        <label class="layui-form-label">逝者姓名</label>
        <div class="layui-input-inline">
          <el-input v-model="selectData.regNameLike" placeholder="请输入逝者姓名" @keydown.enter.native="btnSearch"></el-input>
        </div>
      </div>
      <div class="layui-inline">
        <label class="layui-form-label">开始日期</label>
        <div class="layui-input-inline">
          <el-date-picker v-date v-model="selectData.mehBegDateSta" type="date" placeholder="请选择日期" @change="btnSearch">
          </el-date-picker>
        </div>
      </div>
      <div class="layui-inline">
        <label class="layui-form-label">结束日期</label>
        <div class="layui-input-inline">
          <el-date-picker v-date v-model="selectData.mehBegDateEnd" type="date" placeholder="请选择日期" @change="btnSearch">
          </el-date-picker>
        </div>
      </div>
      <el-button type="primary" @click="btnSearch" :loading="this.$store.state.isloading">搜索</el-button>
      <el-button @click="btnRepeat">重置</el-button>
    </div>
    <!-- 功能列表 -->
    <div style="margin-top:10px;">
      <ul class="toolList">
        <li><el-button type="primary" @click="btnSearch" :loading="this.$store.state.isloading">刷新</el-button></li>
        <li v-if="btnShow(item.BTNVALUE)" v-for="(item, index) in $store.state.btnListCommon" @click="operate(item.BTNVALUE)" :key="index">
          <el-button type="primary" :value="item.BTNSHOWNAME" v-text="item.BTNSHOWNAME"></el-button>
        </li>
      </ul>
    </div>
    <!-- 数据列表 -->
    <dataTable :headList="headList" :dataList="dataList" :pageOrder="pageOrder" @pageChange="pageChange" @selectRow="selectRow"></dataTable>
    <!-- 操作弹窗 -->
    <main-model ref="dialog" @refreshData="getData"></main-model>
  </div>
</template>
<style rel="stylesheet/scss" lang="scss" scoped>
  @import "src/styles/common.scss";
  .el-form-item {
    margin-bottom: 0px;
  }
  .btnCenter {
    text-align: center;
  }
</style>
<script>
  import apiBase from "@/api/apiBase.js";
  import dataTable from "@/components/Table/DataTable";
  import {  formatDate, timeStringToDate } from "@/utils/date.js";
  import { manage } from "@/mixins/managePage.js";
  import mainModel from "./mourningUseMain";
  import { ORDER } from "@/const/orderConst.js";

  const ADD = "新增";
  const MODIFY = "修改";
  const DELETE = "删除";
  const INFO = "详情";
  const BEG = "开始使用";
  const END = "结束使用";
  const REG = "礼厅登记";
  export default {
    name: "mourningUse",
    mixins: [manage],
    data() {
      return {
        memNames: [],
        selectData: {},
        mehData: {},
        regList: [],
        staTime: null,
        endTime: null,
        ischangered: null,
        ischangnb: null,
        //二级下拉选择
        showsecond:null,
        MehselectArea:[],
      };
    },
    methods: {
      //功能操作
      operate(order) {
        this.order = order;
        switch (this.order) {
          case ADD:
          case REG:
          case BEG:
            this.$refs.dialog.open(this.order);
            break;

          case MODIFY:
          case INFO: 
            this.$refs.dialog.open(this.order,this.currentRow.MEHID);
            break;           
          case END:
            if(this.currentRow.MEHENDDATE!=''){
              this.$message({
                message: "该条记录已结束使用",
                type: "error"
              })
            }
            else 
              this.$refs.dialog.open(this.order,this.currentRow.MEHID);
            break;
          case DELETE:
            this.dataDelete();
            break;
          default:
        }
      },

      // 控制是否显示
      btnShow(val){
       if(val == BEG || val == ADD|| val == REG) return true;
       if(this.currentRow){
         if(val == MODIFY || val == DELETE || val == INFO || val == END) return true;
       }
      },
    
      //获取表头
      getMemColumn() {
        apiBase.DoService({},
          apiBase.getApiAdress().Column.GetMemorialUseColumns,
          res => {
            this.headList = res.data.content;
          }
        );
      },
      //获取查询数据
      getData() {
        let Data = JSON.parse(JSON.stringify(this.selectData));
        Data.mehBegDateSta = formatDate(Data.mehBegDateSta, "yyyyMMdd");
        Data.mehBegDateEnd = formatDate(Data.mehBegDateEnd, "yyyyMMdd");
        if(this.MehselectArea.length!=0){
          if(this.MehselectArea.length==1){
          if (this.MehselectArea[0] == "001") {
          }else{
          Data.MemcodnameEquals=this.MehselectArea[this.MehselectArea.length-1]
          } 
          }else{
           Data.MemidEquals =this.MehselectArea[this.MehselectArea.length-1]
          }
        }
        var data = {
          PageIndex: this.pageOrder.currentPage,
          PageSize: this.pageOrder.pageSize,
          OrderBy: this.pageOrder.orderType,
          OrderByType: this.pageOrder.orderSort,
          Data: Data
        };
        apiBase.DoService(data, apiBase.getApiAdress().MemorialUse.Query, res => {
          this.dataList = res.data.content.OutData;
          this.pageOrder.totalNum = res.data.content.RecordCount;
          this.$nextTick(function() {
            let rows = document.getElementsByClassName("el-table__row");
            if(this.ischangered){
              this.dataList.forEach((item,index) => {
                  rows[index].style.color = '#606266';
                  if(item.REGCRETYPE === 'A'){
                      rows[index].style.color = 'red';
                  }
              });
            };
            if(this.ischangnb){
              this.dataList.forEach((item,index) => {
                  rows[index].style.color = '#606266';
                  if(item.REGCRETYPE === 'A'||item.REGCRETYPE === 'C'){
                      rows[index].style.color = '#007eff';
                  }
                  if(item.REGCRETYPE === 'D'){
                      rows[index].style.color = 'red';
                  }
              });
            };
          });
        });
      },
      //删除
      dataDelete() {
        var data = {
          MEHID: this.currentRow.MEHID
        };
        this.$confirm("此操作将永久删除该记录, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(callback => {
          apiBase.DoService(
            data,
            apiBase.getApiAdress().MemorialUse.Delete,
            res => {
              this.getData();
              this.$message({
                message: "删除成功！",
                type: "success"
              });
            }
          );
        }).catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
      
      },
      //重置查询
      btnRepeat() {
        this.selectData = {};
        this.MehselectArea=[];
        this.getData();
      },
     
    },
     //路由的时候刷新组件
    activated: function() {
      this.getData();
    },
    components: {
      mainModel,
      dataTable
    },
    created() {
      //悼念厅下拉是否使用二级下拉(上虞))
      apiBase.DoService(
          {},
          apiBase.getApiAdress().Param.GetIsUseMemSecond,
          res => {
            this.showsecond = res.data.content;
            if(this.showsecond==true){
              return new Promise((resolve,reject)=>{
                apiBase.DoService(
                    {},
                    apiBase.getApiAdress().MemorialUse.QueryMemSecond,
                    res => {
                    let mehTypeData = res.data.content;
                        this.memNames = mehTypeData.map(item => ({
                            label: item.CODALIAS,
                            value: item.CODNAME,
                              children: item.list_MEM.map(itemCau => ({
                              label: itemCau.MEMNAME,
                              value: itemCau.MEMID
                            }))
                        }));
                        this.memNames.unshift({
                        label: "全部",
                        value: "001",
                        children: [{ label: "全部", value: null }]
                        });
                      resolve() 

                    });
                })
            }else{
                  apiBase.DoService(
                    {},
                    apiBase.getApiAdress().MemorialUse.QueryMemorial,
                    res => {
                      this.memNames = res.data.content.map(item => ({
                        label: item.MEMNAME,
                        value: item.MEMID
                      }));
                    }
                  );
                }
          }
      );
      apiBase.DoService({},apiBase.getApiAdress().Param.GetIsDataTableReddening,res => {
        this.ischangered = res.data.content; 
      });
      apiBase.DoService({},apiBase.getApiAdress().Param.GetIsCreChangeRed,res => {
        this.ischangnb = res.data.content; 
      });
      this.getMemColumn();
      this.getData();
    }
  };
</script>